<template>
  <div class="center-container">
    <div v-if="userInfo.status === -1">
      完善信息
    </div>

    <div v-else>
      <div class="record card">
        <div class="card-title">近期学习记录</div>
        <div class="records">
          <div :class="'l'+parseInt(Math.random()*5+1)" v-for="n in 365" :key="n"></div>
        </div>
      </div>
      <div class="flex-box">
        <div class="learn-now card left-main">
          <div class="card-title">
            <div v-for="(item,index) in menu" :key="index" class="card-item" @click="handleSelectedId(item.id)"
                 :class="{'section-chosen': item.id === selectedId}">{{ item.name }}
            </div>
          </div>
          <div class="course-list">
            <div class="list-item course" v-for="course in courses" :key="course.id">
              <div class="flex-box" @click="toStudy(course.id)">
                <div class="cover img-box">
                  <img :src="course.cover" alt="cover">
                </div>
                <div class="info flex-box">
                  <div>
                    <div class="title">{{ course.name }}</div>
                    <div class="status">共{{ course.sectionNum }}节 | 已学{{ course.sectionLearnedNum }}节
                      学完{{ course.sectionLearnedNum / course.sectionNum | percent }}
                    </div>
                  </div>
                  <div class="last">上次学到：{{ course.lastSectionContentName }}</div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="other right-side">
          <div class="account card">
            <div class="card-title">我的账户</div>
          </div>
          <div class="data card">
            <div class="card-title">学习数据</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import constants from "_u/constants";

export default {
  name: "CenterView",
  data() {
    return {
      userInfo: {},
      courses: [],
      menu: [
        {
          id: 1,
          name: '企业课程'
        },
        {
          id: 2,
          name: '我的课程'
        }
      ],
      selectedId: 1
    }
  },
  watch:{
    selectedId(){
      if (this.selectedId===1){
        this.getEEUserCourseList()
      }else {
        this.getUserCourseList()
      }
    }
  },
  mounted() {
    this.userInfo = JSON.parse(window.localStorage.getItem(constants.ACCESS_TOKEN))
    this.getEEUserCourseList()
  },
  methods: {
    getEEUserCourseList() {
      console.log('获取课程列表')
      this.$api.getUserCourseList({
        pageSize: 20,
        pageNow: 1,
        status: 4,
        type: 1,
        process: 2
      }).then(({data}) => {
        console.log("res", data)
        this.courses = data.data.rows;
        this.loading = false;
      });
    },
    getUserCourseList() {
      console.log('获取课程列表')
      this.$api.getUserCourseList({
        pageSize: 20,
        pageNow: 1,
        status: 4,
        type: 0,
        process: 2
      }).then(({data}) => {
        console.log("res", data)
        this.courses = data.data.rows;
        this.loading = false;
      });
    },
    toStudy(courseId) {
      this.$router.push({
        name: "learn-notice",
        params: {courseId: courseId},
      });
    },
    handleSelectedId(id){
      this.selectedId = id
    }
  },
  filters: {
    percent(n) {
      return parseInt(n * 100 + '') + '%';
    }
  }
}
</script>

<style scoped lang="scss">
.card-title {
  display: flex;
  flex-direction: row;

  .card-item {
    font-size: 18px;
    margin-right: 20px;
    padding-bottom: 5px;
    cursor: pointer;
    &:hover{
      color: #6198f3;
      border-bottom: 4px solid #6198f3;
    }
  }
  .section-chosen{
    color: #357FF8;
    border-bottom: 4px solid #357FF8;
  }
}
.course-list{
  cursor: pointer;
}

.record, .account {
  margin-bottom: 20px;
}

.records {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  height: 115px;
  transform: scale(1);

  > div {
    width: 13px;
    height: 13px;
    border: 1px solid white;
    border-bottom-width: 2px;
    border-radius: 3px;
  }

  .l1 {
    background: #dfdfdf;
  }

  .l2 {
    background: #c5c5c5;
  }

  .l3 {
    background: #a1a1a1;
  }

  .l4 {
    background: #828282;
  }

  .l5 {
    background: #666666;
  }
}

.course {
  margin: 20px 0;

  .cover {
    height: 105px;
    width: 187px;
    margin-right: 10px;
  }

  .info {
    flex: 1;
    flex-direction: column;
    justify-content: space-between;
    font-size: 14px;
  }

  .title {
    font-size: 18px;
    margin-bottom: 5px;
  }

  .status {
    color: #888;
  }

  .last {
    color: #888;
    background: #f0f0f0;
    padding: 2px 5px;
    border-radius: 3px;
  }

}

</style>
